#include "../app_status_bar_conf.h"
#include "../inc/app_status_bar_type.h"

#if STATUS_BAR_USE_CONTROL_CENTER && STATUS_BAR_APP_USE

typedef enum
{
    WIDGETS_TYPE_SWITCH,
    WIDGETS_TYPE_SLIDER,
} widgets_type_t;

typedef struct _widgets_paras_t
{
    const void *app_key;
    const char *theme_set;
    const char *theme_get;
    const char *app;
    uint8_t type;
    uint8_t id;
    struct _widgets_paras_t *next;
} widgets_paras_t;

enum
{
    WIDGETS_TYPE_ICON = 1,
    WIDGETS_TYPE_POS,
    WIDGETS_TYPE_THEME_SET,
    WIDGETS_TYPE_THEME_GET,
    WIDGETS_TYPE_APP,
    WIDGETS_TYPE_APP_KEY,
    WIDGETS_TYPE_RANGE,
};

static lv_coord_t _col, _row;
static widgets_paras_t *widgets_list_head = NULL;
static widgets_paras_t *widgets_list_tail = NULL;
static app_status_bar_scr_objs_t *scr_objs = NULL;
extern app_status_bar_scr_objs_t *app_status_bar_get_objs();
extern void status_bar_expand(bool en);
void app_status_bar_control_expand_start();
#if (STATUS_BAR_USE_WIFI_LIST || STATUS_BAR_USE_BT_LIST)
static uint8_t list_expanded = 0; // 0未展开，1展开wifi  2展开蓝牙
#endif

static void get_widgets_define_pos(lv_coord_t *x, lv_coord_t *y, lv_coord_t dx, lv_coord_t dy)
{
    *x = dx;
    *y = dy;
}

static void pressed_cb(lv_event_t *e)
{
    scr_objs->scrolled = 0;
    lv_indev_get_point(lv_event_get_indev(e), &scr_objs->pressed_point);
    if (lv_obj_check_type(e->target, &lv_slider_class))
    {
        scr_objs->pressed_cache = lv_slider_get_value(e->target);
        return;
    }
    if (lv_obj_check_type(e->target, &lv_switch_class))
    {
        scr_objs->pressed_cache = lv_obj_has_state(e->target, LV_STATE_CHECKED);
        return;
    }
}

static void release_cb(lv_event_t *e)
{
    if (lv_obj_check_type(e->target, &lv_switch_class))
    {
#if STATUS_BAR_USE_WIFI_LIST || STATUS_BAR_USE_BT_LIST
        if (list_expanded)
        {
            if (scr_objs->pressed_cache)
                lv_obj_add_state(e->target, LV_STATE_CHECKED);
            else
                lv_obj_clear_state(e->target, LV_STATE_CHECKED);
            return;
        }
#endif

        lv_point_t point;
        lv_indev_get_point(lv_event_get_indev(e), &point);
        if (scr_objs->pressed_point.x != point.x || scr_objs->pressed_point.y != point.y)
        {
            scr_objs->scrolled = 1;
            if (scr_objs->pressed_cache)
                lv_obj_add_state(e->target, LV_STATE_CHECKED);
            else
                lv_obj_clear_state(e->target, LV_STATE_CHECKED);
        }
        else
        {
            lv_event_cb_t cb = (lv_event_cb_t)e->user_data;
            cb(e);
        }
        return;
    }
}

static uint8_t chek_change_en(lv_obj_t *obj)
{
    if (lv_obj_check_type(obj, &lv_slider_class))
    {
        if (scr_objs->dir == LV_DIR_VER)
            return 1;
        lv_slider_set_value(obj, scr_objs->pressed_cache, LV_ANIM_OFF);
        return 0;
    }
    return 0;
}

#if STATUS_BAR_USE_FLASHLIGHT

#if STATUS_BAR_FLASH_LIGHT_USE_SCREEN
static void scr_flashlight_cb(lv_event_t *e)
{
    if (e->code == LV_EVENT_GESTURE)
    {
        lv_obj_add_flag(e->target, LV_OBJ_FLAG_USER_1);
        return;
    }
    if (!lv_obj_has_flag(e->target, LV_OBJ_FLAG_USER_1))
        return;

    lv_obj_del(e->target);
#if STATUS_BAR_USE_BRIGHTNESS
    uint8_t sta = (uint8_t)((int)e->user_data);
    topic_messenger_msg("设置屏幕亮度", &sta, sizeof(sta));
#endif
}
#endif

static void flashlight_cb(lv_event_t *e)
{
    uint8_t sta = lv_obj_has_state(e->target, LV_STATE_CHECKED);
#if STATUS_BAR_FLASH_LIGHT_USE_SCREEN
#if STATUS_BAR_USE_BRIGHTNESS
    topic_messenger_msg("获取屏幕亮度", &sta, sizeof(sta));
#endif
    lv_obj_clear_state(e->target, LV_STATE_CHECKED);
    lv_obj_t *scr = lv_obj_create(lv_layer_sys());
    lv_obj_remove_style_all(scr);
    lv_obj_set_size(scr, LV_HOR_RES, LV_VER_RES);
    lv_obj_clear_flag(scr, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_clear_flag(scr, LV_OBJ_FLAG_GESTURE_BUBBLE);

    lv_obj_set_style_bg_color(scr, lv_color_hex(0xFFFFFF), 0);
    lv_obj_set_style_bg_opa(scr, 255, 0);
    lv_obj_add_event_cb(scr, scr_flashlight_cb, LV_EVENT_GESTURE, (void *)(int)sta);
    lv_obj_add_event_cb(scr, scr_flashlight_cb, LV_EVENT_RELEASED, (void *)(int)sta);
    status_bar_expand(0);

#if STATUS_BAR_USE_BRIGHTNESS
    sta = 100;
    topic_messenger_msg("设置屏幕亮度", &sta, sizeof(sta));
#endif

#else
    topic_messenger_msg("设置手电筒状态", &sta, sizeof(sta));
#endif
}
#endif

#if STATUS_BAR_USE_PRINT_SCREEN
static void prtsc_cb(lv_event_t *e)
{
    lv_obj_clear_state(e->target, LV_STATE_CHECKED);
    status_bar_expand(0);
    topic_messenger_msg("开始截图", NULL, 0);
}
#endif

#if STATUS_BAR_USE_WIFI_LIST
static void wifi_list_cb(lv_event_t *e)
{

    uint8_t sta = lv_obj_has_state(e->target, LV_STATE_CHECKED);
    topic_messenger_msg("设置WIFI状态", &sta, 1);
    lv_obj_t *label0 = lv_obj_get_child(e->target, 1);
    lv_obj_t *label1 = lv_obj_get_child(e->target, 2);
    if (sta)
    {
        lv_label_set_text_static(label0, "WLAN");
        lv_label_set_text_static(label1, "已开启");
    }
    else
    {
        lv_label_set_text_static(label0, "WLAN");
        lv_label_set_text_static(label1, "已关闭");
    }
}
#endif

#if STATUS_BAR_USE_BT_LIST
static void bt_list_cb(lv_event_t *e)
{
    uint8_t sta = lv_obj_has_state(e->target, LV_STATE_CHECKED);
    topic_messenger_msg("设置蓝牙状态", &sta, 1);
    lv_obj_t *label0 = lv_obj_get_child(e->target, 1);
    lv_obj_t *label1 = lv_obj_get_child(e->target, 2);
    if (sta)
    {
        lv_label_set_text_static(label0, "BT");
        lv_label_set_text_static(label1, "已开启");
    }
    else
    {
        lv_label_set_text_static(label0, "BT");
        lv_label_set_text_static(label1, "已关闭");
    }
}

#endif

#if STATUS_BAR_USE_BRIGHTNESS
static void blk_cb(lv_event_t *e)
{
    if (chek_change_en(e->target) == 0)
        return;
    uint8_t blk = lv_slider_get_value(e->target);
    topic_messenger_msg("设置屏幕亮度", &blk, sizeof(blk));
}
#endif

#if STATUS_BAR_USE_VOLUME
static void vol_cb(lv_event_t *e)
{
    if (chek_change_en(e->target) == 0)
        return;
    uint8_t vol = lv_slider_get_value(e->target);
    topic_messenger_msg("设置音量", &vol, sizeof(vol));
}
#endif

#if STATUS_BAR_USE_WIFI_SWITCH
static void wifi_switch_cb(lv_event_t *e)
{
    uint8_t sta = lv_obj_has_state(e->target, LV_STATE_CHECKED);
    if (lv_obj_has_flag(e->target, LV_OBJ_FLAG_USER_4))
    {
        lv_obj_clear_flag(e->target, LV_OBJ_FLAG_USER_4);
        if (sta)
            lv_obj_clear_state(e->target, LV_STATE_CHECKED);
        else
            lv_obj_add_state(e->target, LV_STATE_CHECKED);
        return;
    }
    topic_messenger_msg("设置WIFI状态", &sta, 1);
}
#endif

#if STATUS_BAR_USE_WIFI_AP
static void wifi_ap_cb(lv_event_t *e)
{
    uint8_t sta = lv_obj_has_state(e->target, LV_STATE_CHECKED);
    if (lv_obj_has_flag(e->target, LV_OBJ_FLAG_USER_4))
    {
        lv_obj_clear_flag(e->target, LV_OBJ_FLAG_USER_4);
        if (sta)
            lv_obj_clear_state(e->target, LV_STATE_CHECKED);
        else
            lv_obj_add_state(e->target, LV_STATE_CHECKED);
        return;
    }
    topic_messenger_msg("设置热点状态", &sta, 1);
}
#endif

#if STATUS_BAR_USE_BT_SWITCH
static void bt_switch_cb(lv_event_t *e)
{
    uint8_t sta = lv_obj_has_state(e->target, LV_STATE_CHECKED);
    if (lv_obj_has_flag(e->target, LV_OBJ_FLAG_USER_4))
    {
        lv_obj_clear_flag(e->target, LV_OBJ_FLAG_USER_4);
        if (sta)
            lv_obj_clear_state(e->target, LV_STATE_CHECKED);
        else
            lv_obj_add_state(e->target, LV_STATE_CHECKED);
        return;
    }
    topic_messenger_msg("设置蓝牙状态", &sta, 1);
}
#endif

#if STATUS_BAR_USE_AIRPLANE_MODE
static void airplane_mode_cb(lv_event_t *e)
{
    uint8_t sta = lv_obj_has_state(e->target, LV_STATE_CHECKED);
    if (lv_obj_has_flag(e->target, LV_OBJ_FLAG_USER_4))
    {
        lv_obj_clear_flag(e->target, LV_OBJ_FLAG_USER_4);
        if (sta)
            lv_obj_clear_state(e->target, LV_STATE_CHECKED);
        else
            lv_obj_add_state(e->target, LV_STATE_CHECKED);
        return;
    }
    topic_messenger_msg("设置飞行模式状态", &sta, 1);
}
#endif

#if STATUS_BAR_USE_SHUTDOWN
static void shutdown_cb(lv_event_t *e)
{
    lv_obj_clear_state(e->target, LV_STATE_CHECKED);
    topic_messenger_msg("开始锁屏", NULL, 0);
    status_bar_expand(0);
}
#endif

#if STATUS_BAR_USE_SETTING
static void setting_cb(lv_event_t *e)
{
    lv_obj_clear_state(e->target, LV_STATE_CHECKED);
    status_bar_expand(0);
#if STATUS_BAR_USE_APP_MANAGER
    app_manager_open(0, "设置", (void *)LV_SCR_LOAD_ANIM_FADE_IN, 500, 0);
#endif
}
#endif

static void switch_click_cb(lv_event_t *e)
{
    uint8_t sta = lv_obj_has_state(e->target, LV_STATE_CHECKED);
    if (lv_obj_has_flag(e->target, LV_OBJ_FLAG_USER_4))
    {
        lv_obj_clear_flag(e->target, LV_OBJ_FLAG_USER_4);
        if (sta)
            lv_obj_clear_state(e->target, LV_STATE_CHECKED);
        else
            lv_obj_add_state(e->target, LV_STATE_CHECKED);
        return;
    }

    lv_point_t point;
    lv_indev_get_point(lv_event_get_indev(e), &point);
    if (scr_objs->pressed_point.x != point.x || scr_objs->pressed_point.y != point.y)
    {
        scr_objs->scrolled = 1;
        if (scr_objs->pressed_cache)
            lv_obj_add_state(e->target, LV_STATE_CHECKED);
        else
            lv_obj_clear_state(e->target, LV_STATE_CHECKED);
    }
    else
    {
        widgets_paras_t *paras = (widgets_paras_t *)e->user_data;
        if (paras->theme_set == NULL)
        {
            if (sta)
                lv_obj_clear_state(e->target, LV_STATE_CHECKED);
            else
                lv_obj_add_state(e->target, LV_STATE_CHECKED);
            return;
        }

        topic_messenger_msg(paras->theme_set, &sta, 1);
    }
}

static void ext_long_p_cb(lv_event_t *e)
{
    if (scr_objs->dir == LV_DIR_HOR || scr_objs->dir == LV_DIR_VER)
        return;
    lv_obj_add_flag(e->target, LV_OBJ_FLAG_USER_4);
    widgets_paras_t *paras = (widgets_paras_t *)e->user_data;

    status_bar_expand(0);
    app_manager_open_with_userdata(0, paras->app, (void *)LV_SCR_LOAD_ANIM_FADE_IN, 500, 0, (void *)paras->app_key);
}

// app_manager_open_with_userdata(0, (const char *)e->user_data, (void *)LV_SCR_LOAD_ANIM_FADE_IN, 500, 0, e->user_data);

static void inter_long_p_cb(lv_event_t *e)
{
    if (scr_objs->dir == LV_DIR_HOR || scr_objs->dir == LV_DIR_VER)
        return;
    lv_obj_add_flag(e->target, LV_OBJ_FLAG_USER_4);

    status_bar_expand(0);
    app_manager_open_with_userdata(0, "设置", (void *)LV_SCR_LOAD_ANIM_FADE_IN, 500, 0, (void *)e->user_data);
}

static lv_obj_t *create_switch(lv_coord_t col, lv_coord_t row, const void *img, lv_event_cb_t click_cb, const char *app, void *userdata)
{
    widgets_paras_t *paras = (widgets_paras_t *)userdata;

    lv_obj_t *sw = lv_switch_create(scr_objs->control_center);
    lv_obj_set_style_bg_opa(sw, 0, LV_PART_KNOB);
    lv_color_t color = lv_obj_get_style_bg_color(scr_objs->scr, 0);
    color = lv_color_mix(color, lv_color_hex(0x8F8F8F), 200);
    lv_obj_set_style_bg_color(sw, color, LV_PART_MAIN);
    lv_obj_set_grid_cell(sw, LV_GRID_ALIGN_CENTER, col, 1, LV_GRID_ALIGN_CENTER, row, 1);
    lv_obj_add_flag(sw, LV_OBJ_FLAG_EVENT_BUBBLE);

    lv_obj_set_size(sw, STATUS_BAR_CONTROL_CENTER_WIDGETS_SIZE * 0.42, STATUS_BAR_CONTROL_CENTER_WIDGETS_SIZE * 0.42);
    lv_obj_set_style_radius(sw, LV_RADIUS_CIRCLE, 0);

    if (img != NULL)
    {
        lv_obj_t *_img = lv_img_create(sw);
        lv_img_set_src(_img, img);
        lv_obj_center(_img);
    }

    lv_obj_add_event_cb(sw, pressed_cb, LV_EVENT_PRESSED, NULL);

    if (paras != NULL) // 自定义控件
    {
        lv_obj_add_event_cb(sw, switch_click_cb, LV_EVENT_RELEASED, paras);

        if (paras->app != NULL)
        {
            lv_obj_add_event_cb(sw, ext_long_p_cb, LV_EVENT_LONG_PRESSED, paras);
        }
    }
    else // 内置控件
    {
        if (click_cb != NULL)
            lv_obj_add_event_cb(sw, release_cb, LV_EVENT_RELEASED, click_cb);
        if (app != NULL)
            lv_obj_add_event_cb(sw, inter_long_p_cb, LV_EVENT_LONG_PRESSED, (void *)app);
    }

    lv_obj_clear_flag(sw, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
    return sw;
}

widgets_paras_t *unpack_list(topic_messenger_list_t list, lv_img_t **img, int16_t **range)
{
    widgets_paras_t *paras = lv_mem_alloc(sizeof(widgets_paras_t));
    if (paras == NULL)
        return NULL;

    uint8_t x = 0, y = 0;

    memset(paras, 0, sizeof(widgets_paras_t));
    paras->type = WIDGETS_TYPE_SWITCH;

    while (1)
    {
        uint8_t cnt = topic_messenger_list_get_cnt(list);
        void *data;
        size_t lenth;
        topic_messenger_list_retrieve(list, &data, &lenth);
        if (lenth == WIDGETS_TYPE_APP_KEY)
        {
            paras->app_key = data;
        }
        else if (lenth == WIDGETS_TYPE_APP)
            paras->app = (const char *)data;
        else if (lenth == WIDGETS_TYPE_ICON)
            *img = (lv_img_t *)data;
        else if (lenth == WIDGETS_TYPE_POS)
        {
            uint8_t *pos = (uint8_t *)data;
            x = pos[0];
            y = pos[1];
            get_widgets_define_pos(&_col, &_row, x, y);
        }
        else if (lenth == WIDGETS_TYPE_THEME_GET)
            paras->theme_get = (const char *)data;
        else if (lenth == WIDGETS_TYPE_THEME_SET)
            paras->theme_set = (const char *)data;
        else if (lenth == WIDGETS_TYPE_RANGE)
        {
            if (range != NULL)
                *range = (int16_t *)data;
        }
        if (cnt == 1)
            break;
    }

    if (widgets_list_head == NULL)
        widgets_list_head = paras;
    else
        widgets_list_tail->next = paras;
    widgets_list_tail = paras;
    return paras;
}

static void create_switch_cb(void *userdata, size_t size)
{
    topic_messenger_list_t *list = (topic_messenger_list_t *)userdata;
    lv_img_t *img = NULL;
    widgets_paras_t *paras = unpack_list(*list, &img, NULL);
    if (paras == NULL)
        return;

    lv_obj_t *switch_ = create_switch(_col, _row, img, NULL, NULL, paras);
    paras->id = lv_obj_get_child_id(switch_);
}

static void slider_cb(lv_event_t *e)
{
    if (chek_change_en(e->target) == 0)
        return;
    widgets_paras_t *paras = (widgets_paras_t *)e->user_data;

    if (paras->theme_set == NULL)
    {
        lv_slider_set_value(e->target, scr_objs->pressed_cache, LV_ANIM_OFF);
    }
    else
    {
        int16_t var = lv_slider_get_value(e->target);
        topic_messenger_msg(paras->theme_set, &var, sizeof(var));
    }
}

static lv_obj_t *create_slider(lv_coord_t col, lv_coord_t row, const void *img, lv_event_cb_t cb, int min, int max, void *userdata)
{
    lv_coord_t w = STATUS_BAR_CONTROL_CENTER_WIDGETS_SIZE * 0.45;
    lv_obj_t *slider = lv_slider_create(scr_objs->control_center);
    lv_obj_set_size(slider, w, STATUS_BAR_CONTROL_CENTER_WIDGETS_SIZE);
    lv_obj_add_flag(slider, LV_OBJ_FLAG_EVENT_BUBBLE);
    lv_slider_set_range(slider, min, max);
    lv_slider_set_value(slider, 0, LV_ANIM_OFF);
    lv_obj_set_style_bg_opa(slider, 0, LV_PART_KNOB);
    lv_obj_set_style_bg_opa(slider, 255, LV_PART_MAIN);
    lv_obj_set_style_radius(slider, w / 4, LV_PART_MAIN);
    lv_obj_set_style_radius(slider, 3, LV_PART_INDICATOR);
    lv_color_t color = lv_obj_get_style_bg_color(scr_objs->scr, 0);
    color = lv_color_mix(color, lv_color_hex(0x8F8F8F), 200);
    lv_obj_set_style_bg_color(slider, color, LV_PART_MAIN);
    lv_obj_set_style_bg_color(slider, lv_color_hex(0xffffff), LV_PART_INDICATOR);
    lv_obj_set_grid_cell(slider, LV_GRID_ALIGN_CENTER, col, 1, LV_GRID_ALIGN_CENTER, row, 2);

    if (img != NULL)
    {
        lv_obj_t *_img = lv_img_create(slider);
        lv_img_set_src(_img, img);
        lv_obj_align(_img, LV_ALIGN_BOTTOM_MID, 0, -(w / 4));
    }

    lv_obj_add_event_cb(slider, pressed_cb, LV_EVENT_PRESSED, NULL);

    if (userdata == NULL)
        lv_obj_add_event_cb(slider, cb, LV_EVENT_VALUE_CHANGED, NULL);
    else
        lv_obj_add_event_cb(slider, slider_cb, LV_EVENT_VALUE_CHANGED, userdata);

    lv_obj_clear_flag(slider, LV_OBJ_FLAG_SCROLL_ON_FOCUS);

    return slider;
}

static void create_slider_cb(void *userdata, size_t size)
{
    topic_messenger_list_t *list = (topic_messenger_list_t *)userdata;
    lv_img_t *img = NULL;
    int16_t *range = NULL;
    widgets_paras_t *paras = unpack_list(*list, &img, &range);
    if (paras == NULL)
        return;

    lv_obj_t *slder = create_slider(_col, _row, img, NULL, range[0], range[1], paras);
    paras->id = lv_obj_get_child_id(slder);
}

#if STATUS_BAR_USE_MUSIC

static void music_cb(lv_event_t *e)
{
    uint8_t key = 0; // 0：上一曲，  1：下一曲，  2：暂停，  3：播放
    if (lv_obj_has_flag(e->target, LV_OBJ_FLAG_USER_2))
    {
        key = 1;
    }
    else if (lv_obj_has_flag(e->target, LV_OBJ_FLAG_USER_1))
    {
        if (lv_obj_has_flag(e->target, LV_OBJ_FLAG_USER_3))
            key = 2;
        else
            key = 3;
    }
    topic_messenger_msg("音乐控制", &key, sizeof(key));
}

static void music_set_widgets_cb(uint8_t sta, uint16_t time[2])
{

    lv_obj_t *label_text = lv_obj_get_child(scr_objs->music, 0);
    lv_obj_t *bar = lv_obj_get_child(scr_objs->music, 1);
    lv_obj_t *last = lv_obj_get_child(scr_objs->music, 2);
    lv_obj_t *play = lv_obj_get_child(scr_objs->music, 3);
    lv_obj_t *next = lv_obj_get_child(scr_objs->music, 4);

    if (sta == 0)
    {
        if (!lv_obj_has_flag(scr_objs->music, LV_OBJ_FLAG_USER_1))
            return;
        lv_obj_clear_flag(scr_objs->music, LV_OBJ_FLAG_USER_1);
        lv_obj_clear_flag(last, LV_OBJ_FLAG_CLICKABLE);
        lv_obj_clear_flag(play, LV_OBJ_FLAG_CLICKABLE);
        lv_obj_clear_flag(next, LV_OBJ_FLAG_CLICKABLE);
        lv_obj_set_style_text_color(last, lv_color_hex(0x525252), 0);
        lv_obj_set_style_text_color(play, lv_color_hex(0x525252), 0);
        lv_obj_set_style_text_color(next, lv_color_hex(0x525252), 0);
        lv_label_set_text_static(label_text, "无来源");
        lv_obj_clear_flag(label_text, LV_OBJ_FLAG_USER_1);
        lv_obj_clear_flag(label_text, LV_OBJ_FLAG_USER_2);
        lv_obj_clear_flag(play, LV_OBJ_FLAG_USER_3);
        lv_label_set_text_static(play, STATUS_BAR_SYMBOL_PLAY);
    }
    else
    {
        if (!lv_obj_has_flag(scr_objs->music, LV_OBJ_FLAG_USER_1))
        {
            lv_obj_add_flag(scr_objs->music, LV_OBJ_FLAG_USER_1);
            lv_obj_set_style_text_color(last, lv_color_hex(0xffffff), 0);
            lv_obj_set_style_text_color(play, lv_color_hex(0xffffff), 0);
            lv_obj_set_style_text_color(next, lv_color_hex(0xffffff), 0);
            lv_obj_add_flag(last, LV_OBJ_FLAG_CLICKABLE);
            lv_obj_add_flag(play, LV_OBJ_FLAG_CLICKABLE);
            lv_obj_add_flag(next, LV_OBJ_FLAG_CLICKABLE);
        }
        if (lv_bar_get_value(bar) != time[0])
            lv_bar_set_value(bar, time[0], LV_ANIM_ON);
        if (lv_bar_get_max_value(bar) != time[1])
            lv_bar_set_range(bar, 0, time[1]);

        if (sta == 1 && !lv_obj_has_flag(label_text, LV_OBJ_FLAG_USER_1))
        {
            lv_label_set_text_static(label_text, "休息一下");
            lv_obj_add_flag(label_text, LV_OBJ_FLAG_USER_1);
            lv_obj_clear_flag(label_text, LV_OBJ_FLAG_USER_2);
            lv_obj_clear_flag(play, LV_OBJ_FLAG_USER_3);
            lv_label_set_text_static(play, STATUS_BAR_SYMBOL_PLAY);
        }
        if (sta == 2 && !lv_obj_has_flag(label_text, LV_OBJ_FLAG_USER_2))
        {
            lv_label_set_text_static(label_text, "请欣赏");
            lv_obj_add_flag(label_text, LV_OBJ_FLAG_USER_2);
            lv_obj_clear_flag(label_text, LV_OBJ_FLAG_USER_1);
            lv_obj_add_flag(play, LV_OBJ_FLAG_USER_3);
            lv_label_set_text_static(play, STATUS_BAR_SYMBOL_PAUSE);
        }
    }
}

static void create_music()
{
    scr_objs->music = lv_obj_create(scr_objs->control_center);
    lv_obj_set_size(scr_objs->music, STATUS_BAR_CONTROL_CENTER_WIDGETS_SIZE, STATUS_BAR_CONTROL_CENTER_WIDGETS_SIZE);
    lv_obj_clear_flag(scr_objs->music, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_style_border_width(scr_objs->music, 0, 0);
    lv_obj_add_flag(scr_objs->music, LV_OBJ_FLAG_EVENT_BUBBLE);
    lv_color_t color = lv_obj_get_style_bg_color(scr_objs->scr, 0);
    color = lv_color_mix(color, lv_color_hex(0x8F8F8F), 200);
    lv_obj_set_style_bg_color(scr_objs->music, color, LV_PART_MAIN);
    lv_obj_set_style_radius(scr_objs->music, STATUS_BAR_CONTROL_CENTER_WIDGETS_SIZE * 0.45 / 4, 0);
    lv_obj_set_style_pad_all(scr_objs->music, STATUS_BAR_CONTROL_CENTER_WIDGETS_SIZE * 0.45 / 5, 0);

    get_widgets_define_pos(&_col, &_row, APP_STATUS_BAR_MUSIC_POS);
    lv_obj_set_grid_cell(scr_objs->music, LV_GRID_ALIGN_CENTER, _col, 2, LV_GRID_ALIGN_CENTER, _row, 2);
    lv_obj_clear_flag(scr_objs->music, LV_OBJ_FLAG_SCROLL_ON_FOCUS);

    lv_obj_t *label = lv_label_create(scr_objs->music);
    lv_obj_set_style_text_font(label, &app_status_bar_font_music_text, 0);
    lv_label_set_text_static(label, "无来源");
    lv_obj_align(label, LV_ALIGN_TOP_MID, 0, STATUS_BAR_CONTROL_CENTER_WIDGETS_SIZE * 0.1);
    lv_obj_set_style_text_color(label, lv_color_hex(0xffffff), 0);

    lv_obj_t *bar = lv_bar_create(scr_objs->music);
    lv_obj_set_size(bar, STATUS_BAR_CONTROL_CENTER_WIDGETS_SIZE * 0.8, STATUS_BAR_CONTROL_CENTER_WIDGETS_SIZE * 0.05);
    lv_obj_set_style_radius(bar, 0, 0);
    lv_obj_set_style_radius(bar, 0, LV_PART_INDICATOR);
    lv_bar_set_range(bar, 0, 100);
    lv_bar_set_value(bar, 50, LV_ANIM_OFF);
    lv_obj_align(bar, LV_ALIGN_CENTER, 0, 0);

    label = lv_label_create(scr_objs->music);
    lv_obj_set_style_text_font(label, &app_status_bar_font_music_symbol, 0);
    lv_label_set_text_static(label, STATUS_BAR_SYMBOL_LAST);
    lv_obj_align(label, LV_ALIGN_BOTTOM_LEFT, 0, 0);
    lv_obj_set_style_text_color(label, lv_color_hex(0x525252), 0);
    lv_obj_add_event_cb(label, music_cb, LV_EVENT_CLICKED, NULL);

    label = lv_label_create(scr_objs->music);
    lv_obj_set_style_text_font(label, &app_status_bar_font_music_symbol, 0);
    lv_label_set_text_static(label, STATUS_BAR_SYMBOL_PLAY);
    lv_obj_align(label, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_obj_set_style_text_color(label, lv_color_hex(0x525252), 0);
    lv_obj_add_flag(label, LV_OBJ_FLAG_USER_1);
    lv_obj_add_event_cb(label, music_cb, LV_EVENT_CLICKED, NULL);

    label = lv_label_create(scr_objs->music);
    lv_obj_set_style_text_font(label, &app_status_bar_font_music_symbol, 0);
    lv_label_set_text_static(label, STATUS_BAR_SYMBOL_NEXT);
    lv_obj_align(label, LV_ALIGN_BOTTOM_RIGHT, 0, 0);
    lv_obj_set_style_text_color(label, lv_color_hex(0x525252), 0);
    lv_obj_add_flag(label, LV_OBJ_FLAG_USER_2);
    lv_obj_add_event_cb(label, music_cb, LV_EVENT_CLICKED, NULL);
}
#endif

#if (STATUS_BAR_USE_WIFI_LIST || STATUS_BAR_USE_BT_LIST)

static void anim_end_cb(lv_anim_t *a)
{
    lv_obj_add_flag((lv_obj_t *)a->var, LV_OBJ_FLAG_HIDDEN);
}

static lv_area_t get_screen_coords(lv_obj_t *obj)
{
    lv_area_t area;
    lv_obj_get_coords(obj, &area); // 获取控件在其父容器中的坐标

    lv_obj_t *parent = lv_obj_get_parent(obj); // 获取父容器
    while (parent)
    {
        lv_area_t parent_area;
        lv_obj_get_coords(parent, &parent_area); // 获取父容器的坐标
        area.x1 += parent_area.x1;               // 累加父容器的偏移
        area.y1 += parent_area.y1;
        area.x2 += parent_area.x1;
        area.y2 += parent_area.y1;
        parent = lv_obj_get_parent(parent); // 继续向上查找父容器
    }
    return area; // 返回相对于屏幕的绝对坐标
}

static void list_close_cb(lv_event_t *e)
{
    lv_obj_t **objs = (lv_obj_t **)e->user_data;
    lv_obj_t *list = objs[0];
    lv_obj_t *cont = objs[1];

    lv_obj_clear_flag(scr_objs->control_center, LV_OBJ_FLAG_HIDDEN);
    lv_obj_clear_flag(scr_objs->bar_left, LV_OBJ_FLAG_HIDDEN);
    lv_obj_clear_flag(scr_objs->bar_right, LV_OBJ_FLAG_HIDDEN);
#if STATUS_BAR_USE_TIME
    lv_obj_clear_flag(scr_objs->date, LV_OBJ_FLAG_HIDDEN);
#endif
    lv_obj_clear_flag(scr_objs->scr, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_remove_event_cb(scr_objs->scr, list_close_cb);

    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_var(&a, cont);
    lv_anim_set_time(&a, 200);
    lv_anim_set_values(&a, LV_HOR_RES * 0.85, STATUS_BAR_CONTROL_CENTER_WIDGETS_SIZE);
    lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_width);
    lv_anim_start(&a);

    lv_coord_t height = LV_HOR_RES * 0.85 * 1.5;
    if (height >= (LV_VER_RES * 0.85))
        height = LV_VER_RES * 0.85;
    lv_anim_set_values(&a, height, STATUS_BAR_CONTROL_CENTER_WIDGETS_SIZE * 0.485);
    lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_height);
    lv_anim_start(&a);

    lv_area_t screen_area = get_screen_coords(list);
    screen_area.y1 -= lv_obj_get_scroll_y(scr_objs->control_center);

    lv_anim_set_values(&a, (LV_HOR_RES - LV_HOR_RES * 0.85) / 2, screen_area.x1);
    lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_x);
    lv_anim_start(&a);

    lv_anim_set_values(&a, (LV_VER_RES - height) / 2, screen_area.y1);
    lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_y);
    lv_anim_set_deleted_cb(&a, anim_end_cb);
    lv_anim_start(&a);

    list_expanded = 0;
}

static void list_long_pre_cb(lv_event_t *e)
{
    static lv_obj_t *objs[2];
    objs[0] = e->target;
    objs[1] = (lv_obj_t *)e->user_data;

    lv_point_t point;
    lv_indev_get_point(lv_event_get_indev(e), &point);
    if (scr_objs->pressed_point.x != point.x || scr_objs->pressed_point.y != point.y)
    {
        scr_objs->scrolled = 1;
        return;
        // if (scr_objs->pressed_cache)
        //     lv_obj_add_state(e->target, LV_STATE_CHECKED);
        // else
        //     lv_obj_clear_state(e->target, LV_STATE_CHECKED);
    }

    if (lv_obj_has_state(e->target, LV_STATE_CHECKED))
        lv_obj_add_state(lv_obj_get_child(objs[1], 1), LV_STATE_CHECKED);
    else
        lv_obj_clear_state(lv_obj_get_child(objs[1], 1), LV_STATE_CHECKED);

    list_expanded = lv_obj_has_flag(objs[0], LV_OBJ_FLAG_USER_1) + 1;

    lv_obj_clear_flag((lv_obj_t *)e->user_data, LV_OBJ_FLAG_HIDDEN);
    lv_obj_add_flag(scr_objs->control_center, LV_OBJ_FLAG_HIDDEN);
    lv_obj_add_flag(scr_objs->bar_left, LV_OBJ_FLAG_HIDDEN);
    lv_obj_add_flag(scr_objs->bar_right, LV_OBJ_FLAG_HIDDEN);
#if STATUS_BAR_USE_TIME
    lv_obj_add_flag(scr_objs->date, LV_OBJ_FLAG_HIDDEN);
#endif
    lv_obj_add_flag(scr_objs->scr, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_add_event_cb(scr_objs->scr, list_close_cb, LV_EVENT_CLICKED, &objs);

    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_var(&a, (lv_obj_t *)e->user_data);
    lv_anim_set_time(&a, 200);
    lv_anim_set_values(&a, STATUS_BAR_CONTROL_CENTER_WIDGETS_SIZE, LV_HOR_RES * 0.85);
    lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_width);
    lv_anim_start(&a);

    lv_coord_t height = LV_HOR_RES * 0.85 * 1.5;
    if (height >= (LV_VER_RES * 0.85))
        height = LV_VER_RES * 0.85;
    lv_anim_set_values(&a, STATUS_BAR_CONTROL_CENTER_WIDGETS_SIZE * 0.485, height);
    lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_height);
    lv_anim_start(&a);

    lv_area_t screen_area = get_screen_coords(e->target);
    screen_area.y1 -= lv_obj_get_scroll_y(scr_objs->control_center);

    lv_anim_set_values(&a, screen_area.x1, (LV_HOR_RES - LV_HOR_RES * 0.85) / 2);
    lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_x);
    lv_anim_start(&a);

    lv_anim_set_values(&a, screen_area.y1, (LV_VER_RES - height) / 2);
    lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_y);
    lv_anim_start(&a);
}

static void list_more_setting_cb(lv_event_t *e)
{
    lv_obj_clear_flag(scr_objs->control_center, LV_OBJ_FLAG_HIDDEN);
    lv_obj_clear_flag(scr_objs->bar_left, LV_OBJ_FLAG_HIDDEN);
    lv_obj_clear_flag(scr_objs->bar_right, LV_OBJ_FLAG_HIDDEN);
#if STATUS_BAR_USE_TIME
    lv_obj_clear_flag(scr_objs->date, LV_OBJ_FLAG_HIDDEN);
#endif
    lv_obj_clear_flag(scr_objs->scr, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_remove_event_cb(scr_objs->scr, list_close_cb);

    lv_obj_add_flag(lv_obj_get_parent(e->target), LV_OBJ_FLAG_HIDDEN);

    status_bar_expand(0);
#if STATUS_BAR_USE_APP_MANAGER
    app_manager_open_with_userdata(0, "设置", (void *)LV_SCR_LOAD_ANIM_FADE_IN, 500, 0, e->user_data);
#endif
}

static lv_obj_t *create_list(lv_coord_t col, lv_coord_t row, const void *img, const lv_font_t *font, const char *list_name, lv_event_cb_t cb)
{
    uint8_t font_h = 0;

    lv_color_t color = lv_obj_get_style_bg_color(scr_objs->scr, 0);
    color = lv_color_mix(color, lv_color_hex(0x8F8F8F), 200);

    lv_coord_t h = STATUS_BAR_CONTROL_CENTER_WIDGETS_SIZE * 0.485;
    lv_obj_t *cont = lv_switch_create(scr_objs->control_center);
    lv_obj_set_size(cont, STATUS_BAR_CONTROL_CENTER_WIDGETS_SIZE, h);
    lv_obj_set_style_radius(cont, STATUS_BAR_CONTROL_CENTER_WIDGETS_SIZE * 0.45 / 4, 0);
    lv_obj_set_style_radius(cont, STATUS_BAR_CONTROL_CENTER_WIDGETS_SIZE * 0.45 / 4, LV_PART_INDICATOR);
    lv_obj_set_style_border_width(cont, 0, 0);
    lv_obj_add_flag(cont, LV_OBJ_FLAG_EVENT_BUBBLE);
    lv_obj_clear_flag(cont, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_style_pad_all(cont, 0, 0);
    lv_obj_set_grid_cell(cont, LV_GRID_ALIGN_CENTER, col, 2, LV_GRID_ALIGN_CENTER, row, 1);
    lv_obj_clear_flag(cont, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
    lv_obj_set_style_bg_opa(cont, 0, LV_PART_KNOB);
    lv_obj_set_style_bg_color(cont, color, LV_PART_MAIN);

#if STATUS_BAR_USE_WIFI_LIST && STATUS_BAR_USE_BT_LIST
    if (strcmp(list_name, "WLAN") == 0)
#endif
#if STATUS_BAR_USE_WIFI_LIST
        font_h = app_status_bar_font_wifi_list_height;
#endif
#if STATUS_BAR_USE_WIFI_LIST && STATUS_BAR_USE_BT_LIST
    else
#endif
#if STATUS_BAR_USE_BT_LIST
    {
        font_h = app_status_bar_font_bt_list_height;
        lv_obj_add_flag(cont, LV_OBJ_FLAG_USER_1);
    }
#endif

    lv_obj_t *_img = lv_img_create(cont);
    lv_img_set_src(_img, img);
    lv_obj_align(_img, LV_ALIGN_LEFT_MID, h * 0.2, 0);

    lv_obj_t *label = lv_label_create(cont);
    lv_obj_set_style_text_font(label, font, 0);
    lv_obj_set_style_text_color(label, lv_color_hex(0xffffff), 0);
    lv_label_set_long_mode(label, LV_LABEL_LONG_SCROLL);
    lv_obj_set_width(label, STATUS_BAR_CONTROL_CENTER_WIDGETS_SIZE - h);
    lv_obj_align(label, LV_ALIGN_LEFT_MID, h * 0.8, -(font_h * 0.6));

    label = lv_label_create(cont);
    lv_obj_set_style_text_font(label, font, 0);
    lv_obj_set_style_text_color(label, lv_color_hex(0xB1B1B1), 0);
    lv_obj_align(label, LV_ALIGN_LEFT_MID, h * 0.8, font_h * 0.6);
    lv_label_set_long_mode(label, LV_LABEL_LONG_SCROLL);
    lv_obj_set_width(label, STATUS_BAR_CONTROL_CENTER_WIDGETS_SIZE - h);

    lv_obj_t *page = lv_obj_create(lv_layer_sys());
    lv_coord_t height = LV_HOR_RES * 0.85 * 1.5;
    if (height >= (LV_VER_RES * 0.85))
        height = LV_VER_RES * 0.85;
    lv_obj_set_size(page, LV_HOR_RES * 0.85, height);
    lv_obj_set_style_border_width(page, 0, 0);
    lv_obj_clear_flag(page, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_style_radius(page, STATUS_BAR_CONTROL_CENTER_WIDGETS_SIZE * 0.45 / 3, 0);
    lv_obj_set_style_bg_opa(page, 70, 0);
    lv_obj_set_style_bg_color(page, lv_color_hex(0x0), 0);

    lv_obj_t *list_head = lv_label_create(page);
    lv_label_set_text_static(list_head, list_name);
    lv_obj_set_style_text_font(list_head, font, 0);
    lv_obj_set_height(list_head, font_h * 1.5);
    lv_obj_align(list_head, LV_ALIGN_TOP_LEFT, 0, 0);

    lv_obj_t *_switch = lv_switch_create(page);
    lv_obj_set_size(_switch, font_h * 2.7, font_h * 1.5);
    lv_obj_align(_switch, LV_ALIGN_TOP_RIGHT, 0, 0);
    lv_obj_set_style_bg_color(_switch, color, LV_PART_MAIN);

    lv_obj_t *btn = lv_btn_create(page);
    lv_obj_set_size(btn, LV_HOR_RES * 0.7, font_h * 3);
    lv_obj_align(btn, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_obj_set_style_radius(btn, STATUS_BAR_CONTROL_CENTER_WIDGETS_SIZE * 0.45 / 4, 0);
    lv_obj_set_style_bg_color(btn, color, LV_PART_MAIN);

    label = lv_label_create(btn);
    lv_obj_set_style_text_font(label, font, 0);
    lv_obj_set_style_text_color(label, lv_color_hex(0xffffff), 0);
    lv_label_set_text_static(label, "更多设置");
    lv_obj_center(label);

    lv_obj_add_flag(page, LV_OBJ_FLAG_HIDDEN);

    lv_obj_add_event_cb(cont, pressed_cb, LV_EVENT_PRESSED, NULL);
    lv_obj_add_event_cb(cont, release_cb, LV_EVENT_RELEASED, cb);
    lv_obj_add_event_cb(cont, list_long_pre_cb, LV_EVENT_LONG_PRESSED, page);
    lv_obj_add_event_cb(btn, list_more_setting_cb, LV_EVENT_CLICKED, (void *)list_name);

    return cont;
}
#endif

static void ref_timer_cb(lv_timer_t *e)
{
    app_status_bar_control_expand_start();
}

void app_status_bar_control_hid_start()
{
    if (scr_objs->page_timer != NULL)
    {
        lv_timer_del(scr_objs->page_timer);
        scr_objs->page_timer = NULL;
    }
}

void app_status_bar_control_expand_start()
{

    uint8_t var;

    widgets_paras_t *tmp = widgets_list_head;

    while (tmp != NULL)
    {
        var = 0;
        if (tmp->theme_get != NULL)
        {

            if (tmp->type == WIDGETS_TYPE_SWITCH)
            {
                topic_messenger_msg(tmp->theme_get, &var, sizeof(var));
                if (var)
                    lv_obj_add_state(lv_obj_get_child(scr_objs->control_center, tmp->id), LV_STATE_CHECKED);
                else
                    lv_obj_clear_state(lv_obj_get_child(scr_objs->control_center, tmp->id), LV_STATE_CHECKED);
            }
            else if (tmp->type == WIDGETS_TYPE_SLIDER)
            {
                int16_t val = 0;
                topic_messenger_msg(tmp->theme_get, &val, sizeof(val));
                lv_slider_set_value(lv_obj_get_child(scr_objs->control_center, tmp->id), val, LV_ANIM_OFF);
            }
        }
        tmp = tmp->next;
    }

#if (STATUS_BAR_USE_WIFI_LIST || STATUS_BAR_USE_BT_LIST)
    char name[32];
    uint8_t sta = 0;
    lv_obj_t *label0 = NULL;
    lv_obj_t *label1 = NULL;

    if (list_expanded)
        return;
#endif

#if STATUS_BAR_USE_WIFI_LIST
    var = 5; // 清零
#if STATUS_BAR_USE_TOPIC_MESSENGER
    topic_messenger_msg("获取WIFI状态", &sta, sizeof(sta));
    if (sta)
    {
        topic_messenger_msg("获取WIFI信号", &var, sizeof(var));
        if (var <= 4)
        {
            memset(name, 0, sizeof(name));
            topic_messenger_msg("获取WIFI名称", name, sizeof(name));
        }
    }
#endif
    label0 = lv_obj_get_child(scr_objs->wifi_list, 1);
    label1 = lv_obj_get_child(scr_objs->wifi_list, 2);

    if (sta)
    {
        if (var <= 4)
        {
            lv_label_set_text(label0, name);
            lv_label_set_text_static(label1, "已连接");
        }
        else
        {
            lv_label_set_text_static(label0, "WLAN");
            lv_label_set_text_static(label1, "已开启");
        }
        lv_obj_add_state(scr_objs->wifi_list, LV_STATE_CHECKED);
    }
    else
    {
        lv_label_set_text_static(label0, "WLAN");
        lv_label_set_text_static(label1, "已关闭");
        lv_obj_clear_state(scr_objs->wifi_list, LV_STATE_CHECKED);
    }
    var = 0;
    sta = 0; // 清零
    name[0] = 0;
#endif

#if STATUS_BAR_USE_BT_LIST
    var = 0; // 清零
#if STATUS_BAR_USE_TOPIC_MESSENGER
    topic_messenger_msg("获取蓝牙状态", &sta, sizeof(sta));
    if (sta)
    {
        topic_messenger_msg("获取蓝牙信号", &var, sizeof(var));
        if (var)
        {
            memset(name, 0, sizeof(name));
            topic_messenger_msg("获取蓝牙名称", name, sizeof(name));
        }
    }
#endif
    label0 = lv_obj_get_child(scr_objs->bt_list, 1);
    label1 = lv_obj_get_child(scr_objs->bt_list, 2);

    if (sta)
    {
        if (var <= 4)
        {
            lv_label_set_text(label0, name);
            lv_label_set_text_static(label1, "已连接");
        }
        else
        {
            lv_label_set_text_static(label0, "BT");
            lv_label_set_text_static(label1, "已开启");
        }
        lv_obj_add_state(scr_objs->bt_list, LV_STATE_CHECKED);
    }
    else
    {
        lv_label_set_text_static(label0, "BT");
        lv_label_set_text_static(label1, "已关闭");
        lv_obj_clear_state(scr_objs->bt_list, LV_STATE_CHECKED);
    }
#endif

#if STATUS_BAR_USE_MUSIC
    var = 0;                                                // 清零
#if STATUS_BAR_USE_TOPIC_MESSENGER                          // music_cb
    topic_messenger_msg("获取音乐状态", &var, sizeof(var)); // 0无来源，1：暂停中，2：播放中
    uint16_t time[2] = {50, 100};                           // 0:now,1:end
    if (var)
        topic_messenger_msg("获取音乐时间", time, sizeof(time));
    music_set_widgets_cb(var, time);
#endif
#endif

#if STATUS_BAR_USE_BRIGHTNESS
    var = 0; // 清零
#if STATUS_BAR_USE_TOPIC_MESSENGER
    topic_messenger_msg("获取屏幕亮度", &var, sizeof(var));
#endif
    if (lv_slider_get_value(scr_objs->brightness) != var)
        lv_slider_set_value(scr_objs->brightness, var, LV_ANIM_OFF);
#endif

#if STATUS_BAR_USE_VOLUME
    var = 0; // 清零
#if STATUS_BAR_USE_TOPIC_MESSENGER
    topic_messenger_msg("获取音量", &var, sizeof(var));
#endif
    if (lv_slider_get_value(scr_objs->volume) != var)
        lv_slider_set_value(scr_objs->volume, var, LV_ANIM_OFF);
#endif

#if (STATUS_BAR_USE_FLASHLIGHT && !STATUS_BAR_FLASH_LIGHT_USE_SCREEN)
    var = 0; // 清零
#if STATUS_BAR_USE_TOPIC_MESSENGER
    topic_messenger_msg("获取手电筒状态", &var, sizeof(var));
#endif
    if (var != lv_obj_has_state(scr_objs->flashlight, LV_STATE_CHECKED))
    {
        if (var)
            lv_obj_add_state(scr_objs->flashlight, LV_STATE_CHECKED);
        else
            lv_obj_clear_state(scr_objs->flashlight, LV_STATE_CHECKED);
    }
#endif

#if STATUS_BAR_USE_WIFI_SWITCH
    var = 0; // 清零
#if STATUS_BAR_USE_TOPIC_MESSENGER
    topic_messenger_msg("获取WIFI状态", &var, sizeof(var));
#endif
    if (var != lv_obj_has_state(scr_objs->wifi_switch, LV_STATE_CHECKED))
    {
        if (var)
            lv_obj_add_state(scr_objs->wifi_switch, LV_STATE_CHECKED);
        else
            lv_obj_clear_state(scr_objs->wifi_switch, LV_STATE_CHECKED);
    }
#endif

#if STATUS_BAR_USE_BT_SWITCH
    var = 0; // 清零
#if STATUS_BAR_USE_TOPIC_MESSENGER
    topic_messenger_msg("获取蓝牙状态", &var, sizeof(var));
#endif
    if (var != lv_obj_has_state(scr_objs->bt_switch, LV_STATE_CHECKED))
    {
        if (var)
            lv_obj_add_state(scr_objs->bt_switch, LV_STATE_CHECKED);
        else
            lv_obj_clear_state(scr_objs->bt_switch, LV_STATE_CHECKED);
    }
#endif

#if STATUS_BAR_USE_WIFI_AP
    var = 0; // 清零
#if STATUS_BAR_USE_TOPIC_MESSENGER
    topic_messenger_msg("获取热点状态", &var, sizeof(var));
#endif
    if (var != lv_obj_has_state(scr_objs->wifi_ap, LV_STATE_CHECKED))
    {
        if (var)
            lv_obj_add_state(scr_objs->wifi_ap, LV_STATE_CHECKED);
        else
            lv_obj_clear_state(scr_objs->wifi_ap, LV_STATE_CHECKED);
    }
#endif

#if STATUS_BAR_USE_AIRPLANE_MODE
    var = 0;
#if STATUS_BAR_USE_TOPIC_MESSENGER
    topic_messenger_msg("获取飞行模式状态", &var, sizeof(var));
#endif
    if (var != lv_obj_has_state(scr_objs->airplane_mode, LV_STATE_CHECKED))
    {
        if (var)
            lv_obj_add_state(scr_objs->airplane_mode, LV_STATE_CHECKED);
        else
            lv_obj_clear_state(scr_objs->airplane_mode, LV_STATE_CHECKED);
    }

#endif

    if (scr_objs->page_timer == NULL)
    {
        scr_objs->page_timer = lv_timer_create(ref_timer_cb, 200, NULL);
        lv_timer_set_repeat_count(scr_objs->page_timer, -1);
#if STATUS_BAR_USE_WIFI_LIST || STATUS_BAR_USE_BT_LIST
        list_expanded = 0;
#endif
    }
}

void app_status_bar_control_init(void)
{

    scr_objs = app_status_bar_get_objs();

#if STATUS_BAR_USE_WIFI_LIST
    get_widgets_define_pos(&_col, &_row, APP_STATUS_BAR_WIFI_LIST_POS);
    scr_objs->wifi_list = create_list(_col, _row, &app_status_bar_img_wifi_list, &app_status_bar_font_wifi_list, "WLAN", wifi_list_cb);
#endif

#if STATUS_BAR_USE_BT_LIST
    get_widgets_define_pos(&_col, &_row, APP_STATUS_BAR_BT_LIST_POS);
    scr_objs->bt_list = create_list(_col, _row, &app_status_bar_img_bt_list, &app_status_bar_font_bt_list, "BT", bt_list_cb);
#endif

#if STATUS_BAR_USE_MUSIC
    create_music();
#endif

#if STATUS_BAR_USE_BRIGHTNESS
    get_widgets_define_pos(&_col, &_row, APP_STATUS_BAR_BRIGHTNESS_POS);
    scr_objs->brightness = create_slider(_col, _row, &app_status_bar_img_brightness, blk_cb, 0, 100, NULL);
#endif

#if STATUS_BAR_USE_VOLUME
    get_widgets_define_pos(&_col, &_row, APP_STATUS_BAR_VOLUME_POS);
    scr_objs->volume = create_slider(_col, _row, &app_status_bar_img_volume, vol_cb, 0, 100, NULL);
#endif

#if STATUS_BAR_USE_FLASHLIGHT
    get_widgets_define_pos(&_col, &_row, APP_STATUS_BAR_FLASHLIGHT_POS);
#if !STATUS_BAR_FLASH_LIGHT_USE_SCREEN
    scr_objs->flashlight =
#endif
        create_switch(_col, _row, &app_status_bar_img_flashlight, flashlight_cb, NULL, NULL);
#endif

#if STATUS_BAR_USE_PRINT_SCREEN
    get_widgets_define_pos(&_col, &_row, APP_STATUS_BAR_PRINT_SCREEN_POS);
    create_switch(_col, _row, &app_status_bar_img_prtsc, prtsc_cb, NULL, NULL);
#endif

#if STATUS_BAR_USE_WIFI_SWITCH
    get_widgets_define_pos(&_col, &_row, APP_STATUS_BAR_WIFI_SWITCH_POS);
    scr_objs->wifi_switch = create_switch(_col, _row, &app_status_bar_img_wifi_switch, wifi_switch_cb, "WLAN", NULL);
#endif

#if STATUS_BAR_USE_BT_SWITCH
    get_widgets_define_pos(&_col, &_row, APP_STATUS_BAR_BT_SWITCH_POS);
    scr_objs->bt_switch = create_switch(_col, _row, &app_status_bar_img_bt_switch, bt_switch_cb, "蓝牙", NULL);
#endif

#if STATUS_BAR_USE_WIFI_AP
    get_widgets_define_pos(&_col, &_row, APP_STATUS_BAR_WIFI_AP_POS);
    scr_objs->wifi_ap = create_switch(_col, _row, &app_status_bar_img_wifi_ap, wifi_ap_cb, "热点", NULL);
#endif

#if STATUS_BAR_USE_AIRPLANE_MODE
    get_widgets_define_pos(&_col, &_row, APP_STATUS_BAR_AIRPLANE_MODE_POS);
    scr_objs->airplane_mode = create_switch(_col, _row, &app_status_bar_img_airplane, airplane_mode_cb, "飞行模式 ", NULL);
#endif

#if STATUS_BAR_USE_SHUTDOWN
    get_widgets_define_pos(&_col, &_row, APP_STATUS_BAR_SHUTDOWN_POS);
    create_switch(_col, _row, &app_status_bar_img_shutdown, shutdown_cb, NULL, NULL);
#endif

#if STATUS_BAR_USE_SETTING
    get_widgets_define_pos(&_col, &_row, APP_STATUS_BAR_SETTING_POS);
    create_switch(_col, _row, &app_status_bar_img_setting, setting_cb, NULL, NULL);
#endif

    topic_messenger_subscribe(NULL, "状态栏创建开关", create_switch_cb);
    topic_messenger_subscribe(NULL, "状态栏创建滑动条", create_slider_cb);
}

#endif
